<?php
/**
 * 在AController 加载此函数或者放在helper类，都是一样使用
 * 自定义方法在此
 */


/**
 * test
 * @return [type] [description]
 */
function testtest(){
	echo 'aaa';
}

/**
 * 获取文件类型
 * @param  [type] $type [description]
 * @return [type]       [description]
 */
function getFileType($file_type = null){
	$type = '';
	if (!$file_type) {
		return $type;
	}

    if (preg_match('#jpeg#i', $file_type)) {
        $type = '.jpg';
    } elseif (preg_match('#png#i', $file_type)) {
        $type = '.png';
    } elseif (preg_match('#jpg#i', $file_type)) {
        $type = '.jpg';
    } 

    return $type;	
}
//发送短信的方法
/*function send_sms() {
    $url = 'http://sendcloud.sohu.com/webapi/mail.send.json';


    $param = array(
        'smsUser' => 'ling916_test_O5L5NJ', 
        'templateId' => '1',
        'msgType' => '0',
        'phone' => '15868152817',
        'vars' => '{"%code%":"123456"}'
    );

    $sParamStr = "";
    ksort($param);
    foreach ($param as $sKey => $sValue) {
        $sParamStr .= $sKey . '=' . $sValue . '&';
    }

    $sParamStr = trim($sParamStr, '&');
    $smskey = "VS19iA8zPQjpn1v0";
    $sSignature = md5($smskey."&".$sParamStr."&".$smskey);


    $param = array(
        'smsUser' => 'ling916_test_O5L5NJ', 
        'templateId' => '1',
        'msgType' => '0',
        'phone' => '15868152817',
        'vars' => '{"%code%":"123456"}',
        'signature' => $sSignature
    );

    $data = http_build_query($param);
    echo $data;

    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-Type:application/x-www-form-urlencoded',
            'content' => $data

    ));
    $context  = stream_context_create($options);
    $result = file_get_contents($url, FILE_TEXT, $context);

    return $result;
}*/
//PHPexcel


/**
*导出Execl表格
*@param $expFileName  string  导出文件名称
*@param $expTitle  string  表格中页签的名称
*@param $expCellName  array  列名
*@param $expTableData  array  表格数据
*/
function exportExcel($expFileName,$expTitle,$expCellName,$expTableData){
    require dirname(dirname(__FILE__)).'/common/components/PHPExcel1.8/PHPExcel.php';
    ob_clean();
    
    $fileName = iconv('UTF-8','GB2312',$expFileName);
    $xlsTitle = $expTitle;
    $cellNum = count($expCellName);
    $dataNum = count($expTableData);
    
    $objPHPExcel=new \PHPExcel();
    
    $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
    
    $objPHPExcel->getActiveSheet(0)->setTitle($expTitle); //设置页签名称
    for($i=0;$i<$cellNum;$i++){
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $expCellName[$i][1]); 
    }

    for($i=0;$i<$dataNum;$i++){
        for($j=0;$j<$cellNum;$j++){
            $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), $expTableData[$i][$expCellName[$j][0]]);
        }             
    }
    
    header('pragma:public');
    header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
    header("Content-Disposition:attachment;charset=utf-8;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output'); 
    
    return true;
}

?>